define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wechat/drafts/index' + location.search,
                    del_url: 'wechat/drafts/del',
                    multi_url: 'wechat/drafts/multi',
                    import_url: 'wechat/drafts/import',
                    edit_url: 'wechat/drafts/edit',
                    preview_url: 'wechat/drafts/preview'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'title',
                            title: __('Title'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'cover',
                            title: '封面2',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row) {
                                var result = "<a href=" + row.cover + " target='_blank'><img src='" + row.cover + "' class='cover'></a>";

                                return row.cover ? result : '';
                            }
                        },
                        {
                            field: 'author',
                            title: '作者',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'source_url',
                            title: '来源',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row) {
                                var result = "<a href=" + row.source_url + " target='_blank'>" + row.source_url + "</a>";

                                return result;
                            }
                        },
                        {
                            field: 'summary',
                            title: '摘要',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'state',
                            title: '文章集合',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row) {
                                var result = '';
                                row.articles.forEach(item => {
                                    result += "<p><a href='/panel.php/wechat/articles/edit/ids/" + item.id + "' target='_blank'>" + item.title + "</p>"
                                })
                                return result;
                            }
                        },

                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'preview',
                                    text: '预览', // 按钮文字，语言在对应的lang文件设置
                                    title: '预览', // 提示
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-space-shuttle', // 更换图标
                                    extend: 'data-area=["550px","450px"]',
                                    url: $.fn.bootstrapTable.defaults.extend.edit_url,
                                },
                                {
                                    name: 'del',
                                    text: __('Del'), // 按钮文字
                                    title: __('Del'), // 提示
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    icon: 'fa fa-trash', // 更换图标
                                    url: $.fn.bootstrapTable.defaults.extend.del_url,
                                    confirm: '确认删除吗？',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh');
                                    }
                                }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on("click", "#btn-draft", function () {
                var ids = Table.api.selectedids(table, true);
                // var   selectedData = Table.api.selecteddata(table, true);
                if (ids.length >= 8) {
                    Backend.api.toastr.error('最多选择8条');
                    return
                }
                var index = layer.load();
                $.ajax({
                    url: "wechat/drafts/makeDraft",
                    type: 'post',
                    dataType: 'json',
                    data: {ids: ids.join(',')},
                    success: function (ret) {
                        console.log('ret', ret)
                        ret.code === 0 && Backend.api.toastr.error(ret.msg);
                        ret.code === 1 && Backend.api.toastr.success(ret.msg);

                        layer.close(index);
                        table.bootstrapTable('refresh');

                    }, error: function (e) {
                        Backend.api.toastr.error(e.msg);
                        layer.close(index);
                    }
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
